    <?php

    /**
     * WIDGET BOUTONS
     */
    class wpspo_WidgetBtn extends WP_Widget {

        /**
         * Register widget with WordPress.
         */
        public function __construct() {
            parent::__construct(
                'wpspo_widget', // Base ID
                'SponsorPress Boutons', // Name
                array( 'description' => __( 'Button for SponsorPress', 'sponsorpress' ), ) // Args
            );
        }

        /**
         * Front-end display of widget.
         *
         * @see WP_Widget::widget()
         *
         * @param array $args     Widget arguments.
         * @param array $instance Saved values from database.
         */
        public function widget( $args, $instance ) {
            extract( $args );
            $title = apply_filters( 'widget_title', $instance['title'] );
            $width = apply_filters( 'widget_width', $instance['width'] );
            $nb = apply_filters( 'widget_nb', $instance['nb'] );
            $printBouton = '';

            for($p=1; $p<=$nb; $p++) { 
                $btn[$p] = apply_filters( 'widget_btn'.$p, $instance['bouton'.$p] );
                $text[$p]  = apply_filters( 'widget_btn'.$p, $instance['text'.$p] );
                $link[$p]  = apply_filters( 'widget_btn'.$p, $instance['link'.$p] );
                $color[$p]  = apply_filters( 'widget_btn'.$p, $instance['color'.$p] );

                if($btn[$p]==1) { $printBouton .= wpspobutton_widget_print($text[$p], $width, $color[$p], $link[$p].'/?projet='.get_the_ID()); }
            }

            echo $before_widget;
            if ( ! empty( $title ) )
                echo $before_title . $title . $after_title;
            //echo __( 'Hello, World!', 'sponsorpress' );
            echo $printBouton;
            echo $after_widget;
        }

        /**
         * Sanitize widget form values as they are saved.
         *
         * @see WP_Widget::update()
         *
         * @param array $new_instance Values just sent to be saved.
         * @param array $old_instance Previously saved values from database.
         *
         * @return array Updated safe values to be saved.
         */
        public function update( $new_instance, $old_instance ) {

            $instance = array();
            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['width'] = strip_tags( $new_instance['width'] );
            $instance['nb'] = strip_tags( $new_instance['nb'] );

            for($ii=1; $ii<=$instance['nb']; $ii++) { 
                $instance['bouton'.$ii] = strip_tags( $new_instance['bouton'.$ii] );
                $instance['text'.$ii] = strip_tags( $new_instance['text'.$ii] );
                $instance['link'.$ii] = strip_tags( $new_instance['link'.$ii] );
                $instance['color'.$ii] = strip_tags( $new_instance['color'.$ii] );
            }

            return $instance;
        }

        /**
         * Back-end widget form.
         *
         * @see WP_Widget::form()
         *
         * @param array $instance Previously saved values from database.
         */
        public function form( $instance ) {
            if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'sponsorpress' ); }
            if ( isset( $instance[ 'width' ] ) ) { $width = $instance[ 'width' ]; } else { $width = 160; }
            if ( isset( $instance[ 'nb' ] ) ) { $nb = $instance[ 'nb' ]; } else { $nb = 3; }

            ?>
            <script type="text/javascript">

                //<![CDATA[
                    jQuery(document).ready(function()
                    {
                        // colorpicker field
                        jQuery('.wpspo-color-picker').each(function(){
                            var $this = jQuery(this),
                                id = $this.attr('rel');

                            $this.farbtastic('#' + id);
                        });
                    });
                //]]>   
              </script>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'sponsorpress' ); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
                <label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php _e( 'Width:', 'sponsorpress' ); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id( 'width' ); ?>" name="<?php echo $this->get_field_name( 'width' ); ?>" type="text" value="<?php echo esc_attr( $width ); ?>" />
                <label for="<?php echo $this->get_field_id( 'nb' ); ?>"><?php _e( 'Number of buttons:', 'sponsorpress' ); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id( 'nb' ); ?>" name="<?php echo $this->get_field_name( 'nb' ); ?>" type="text" value="<?php echo esc_attr( $nb ); ?>" />

            </p>
            <?php 
                for($i=1; $i<=$nb; $i++) { 

                    if ( isset( $instance[ 'bouton'.$i ] ) ) { $btn[$i] = $instance[ 'bouton'.$i ]; } else { $btn[$i] = 0; }
                    if ( isset( $instance[ 'text'.$i ] ) ) { $text[$i] = $instance[ 'text'.$i ]; } else { $text[$i] = __( 'My button '.$i, 'sponsorpress' ); }
                    if ( isset( $instance[ 'link'.$i ] ) ) { $link[$i] = $instance[ 'link'.$i ]; } else { $link[$i] = '/'; }
                    if ( isset( $instance[ 'color'.$i ] ) ) { $color[$i] = $instance[ 'color'.$i ]; } else { $color[$i] = '#E15821'; }
            ?>
            <p>
                <input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id( 'bouton'.$i ); ?>" name="<?php echo $this->get_field_name( 'bouton'.$i ); ?>" value="1" <?php if($btn[$i]=='1') { echo ' checked'; } ?> /> <label for="<?php echo $this->get_field_id( 'bouton'.$i ); ?>"><?php _e('Display button', 'sponsorpress'); ?> <?php echo $i ?> ?</label></p>
            <p>
                <label for="<?php echo $this->get_field_id( 'text1' ); ?>"><?php _e('Button texte', 'sponsorpress'); ?> <?php echo $i ?> :</label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'text'.$i ); ?>" name="<?php echo $this->get_field_name( 'text'.$i ); ?>" type="text" value="<?php echo esc_attr( $text[$i] ); ?>" /> 
                <label for="<?php echo $this->get_field_id( 'link'.$î ); ?>"><?php _e('Button link', 'sponsorpress'); ?> <?php echo $i ?> :</label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'link'.$i ); ?>" name="<?php echo $this->get_field_name( 'link'.$i ); ?>" type="text" value="<?php echo esc_attr( $link[$i] ); ?>" />
                <label for="<?php echo $this->get_field_id('color'.$i); ?>"><?php _e('Button color', 'sponsorpress'); ?> <?php echo $i ?> :</label> 
                <input class="widefat" id="<?php echo $this->get_field_id('color'.$i); ?>" name="<?php echo $this->get_field_name('color'.$i); ?>" type="text" value="<?php if($color[$i]) { echo $color[$i]; } else { echo '#fff'; } ?>" />
                <div class="wpspo-color-picker" rel="<?php echo $this->get_field_id('color'.$i); ?>"></div>

            <hr />

            </p>
            <?php } 
        }

    } // class Foo_Widget
    //
    // register Foo_Widget widget
    add_action( 'widgets_init', create_function( '', 'register_widget( "wpspo_WidgetBtn" );' ) );


    function wpspo_load_color_picker_script() {
        wp_enqueue_script('farbtastic');
    }
    function wpspo_load_color_picker_style() {
        wp_enqueue_style('farbtastic');	
    }
    add_action('admin_print_scripts-widgets.php', 'wpspo_load_color_picker_script');
    add_action('admin_print_styles-widgets.php', 'wpspo_load_color_picker_style');


    /*
     * WIDGET JAUGE PROJETS
     */
    class wpspo_WidgetJauge extends WP_Widget {

        /**
         * Register widget with WordPress.
         */
        public function __construct() {
            parent::__construct(
                'wpspo_jauge_projet', // Base ID
                'SponsorPress Jauge Projets', // Name
                array( 'description' => __( 'Displays a gauge for the current project', 'sponsorpress' ), ) // Args
            );
        }

        /**
         * Front-end display of widget.
         *
         * @see WP_Widget::widget()
         *
         * @param array $args     Widget arguments.
         * @param array $instance Saved values from database.
         */
        public function widget( $args, $instance ) {

            global $post;
            extract( $args );
            $title = apply_filters( 'widget_title', $instance['title'] );
            $texte = apply_filters( 'widget_texte', $instance['texte'] );
            $color = apply_filters( 'widget_color', $instance['color'] );
            $type = apply_filters( 'widget_type', $instance['type'] );
            $printJauge = '';


            if($post->ID && is_singular('projet')) {

                $valueObjectif = get_post_meta($post->ID, 'wpspo_boxprojets', true); // Récupère l'objectif cible
                if($valueObjectif>0) {
                    $totalPromesse = WPSPO_Promesse::get_sum($post->ID, 1);  // Récupère les promesses FIXES
                    
                    //$totalPromesse = 500;
                    $ratio = round(( ($totalPromesse*100)/$valueObjectif ));
                    $ratio2 = (95-$ratio);
                    if($ratio2<(-15)) { $ratio2 = -15; }

                    $totalPromesse = wpspo_price($totalPromesse);
                    $valueObjectif = wpspo_price($valueObjectif);
                    
                    $texte = str_replace('%PROMESSE%', $totalPromesse, $texte);
                    $texte = str_replace('%OBJECTIF%', $valueObjectif, $texte);
                    $texte = str_replace('%SAUT%', '<br />', $texte);

                    if($type==1) {
                        $printJauge = '
                            <div id="wpspo-cadre">
                                <div id="wpspo-cadre-parent">
                                    <div id="wpspo-cadre-img_jauge" style="background-color:'.$color.';top:'.$ratio2.'%;"></div>
                                    <img id="wpspo-cadre-img_fond" class="wpspo-cadre-superpose" src="/wp-content/plugins/'.WPSPO_NAME_DIR.'/wpspo-images/wpspo-jauge-fond.png" />
                                </div>
                                <div id="wpspo-cadre-infos">'.$texte.'</div>
                            </div><div class="clear"></div>';
                    }
                    if($type==2) {
                        if($ratio==0) { $ratio = 1; }

                        $printJauge = '<style>
                                .wpspo-progress-color span {
                                    background-color: '.$color.';   
                                }
                            </style>
                            <div id="wpspo-cadre">'.$texte.'<br />';
                        $printJauge .= '<div class="wpspo-progress-bar wpspo-progress-color shine">
                                            <span style="width: '.$ratio.'%"></span>
                                        </div>';
                        $printJauge .= '</div><div class="clear"></div>';
                    }
                    if($type==3) {
                        $printJauge = '<div id="progress_meter" >
                            <ul>
                                <li id="c1_r"><p><span class="pie_left"></span></p></li>
                                <li id="c1_l"><p><span class="pie_right"></span></p></li>
                            </ul>
                            <div id="center-circle">
                                <span>75%</span>
                            </div>
                        </div>';
                    }
                    echo $before_widget;
                    if ( ! empty( $title ) )
                        echo $before_title . $title . $after_title;
                    //echo __( 'Hello, World!', 'sponsorpress' );
                    echo $printJauge;
                    echo $after_widget;
                }
            }
        }

        /**
         * Sanitize widget form values as they are saved.
         *
         * @see WP_Widget::update()
         *
         * @param array $new_instance Values just sent to be saved.
         * @param array $old_instance Previously saved values from database.
         *
         * @return array Updated safe values to be saved.
         */
        public function update( $new_instance, $old_instance ) {

            $instance = array();
            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['texte'] = strip_tags( $new_instance['texte'] );
            $instance['color'] = strip_tags( $new_instance['color'] );
            $instance['type'] = strip_tags( $new_instance['type'] );

            return $instance;
        }

        /**
         * Back-end widget form.
         *
         * @see WP_Widget::form()
         *
         * @param array $instance Previously saved values from database.
         */
        public function form( $instance ) {

            if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'Progression', 'sponsorpress' ); }
            if ( isset( $instance[ 'texte' ] ) ) { $texte = $instance[ 'texte' ]; } else { $texte = __( 'Goal:', 'sponsorpress' ).' %OBJECTIF%'.get_wpspo_currency().'%SAUT%'.__('Total pledges : %PROMESSE%', 'sponsorpress' ).get_wpspo_currency(); }
            if ( isset( $instance[ 'color' ] ) ) { $color = $instance[ 'color' ]; } else { $color = '#cc0000'; }
            if ( isset( $instance[ 'type' ] ) ) { $type = $instance[ 'type' ]; } else { $type = 2; }

            ?>
            <script type="text/javascript">

                //<![CDATA[
                    jQuery(document).ready(function()
                    {
                        // colorpicker field
                        jQuery('.color-picker-jauge').each(function(){
                            var $this = jQuery(this),
                                id = $this.attr('rel');

                            $this.farbtastic('#' + id);
                        });
                    });
                //]]>   
              </script>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><strong><?php _e( 'Title:', 'sponsorpress' ); ?></strong></label> 
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'texte' ); ?>"><strong><?php _e( 'Text:', 'sponsorpress' ); ?></strong></label><br />
                <small><?php _e('Promise to %PROMESSE% to display the total promises, %OBJECTIF% to display the project objective and %SAUT% for newline.', 'sponsorpress'); ?></small>
            </p>
            <p>
                <textarea class="widefat" id="<?php echo $this->get_field_id( 'texte' ); ?>" name="<?php echo $this->get_field_name( 'texte' ); ?>"><?php echo esc_attr( $texte ); ?></textarea>
            </p>
            <p><strong>Type :</strong> <br />
                <input type="checkbox" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="1" <?php if(esc_attr( $type )==1) { echo "checked"; } ?> /> <?php _e( 'Barometer', 'sponsorpress' ); ?> <br />
                <input type="checkbox" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="2" <?php if(esc_attr( $type )==2) { echo "checked"; } ?> /> <?php _e('Bar', 'sponsorpress' ); ?><br />
                <input type="checkbox" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="3" <?php if(esc_attr( $type )==3) { echo "checked"; } ?> /> <?php _e('Circle', 'sponsorpress' ); ?>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'color' ); ?>"><strong><?php _e( 'Color:' , 'sponsorpress'); ?>(<?php echo esc_attr( $color ); ?>)</strong></label> 
                <input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" />
                <div class="color-picker-jauge" rel="<?php echo $this->get_field_id('color'); ?>"></div>

            </p>

            <?php 
        }

    } // class Foo_Widget
    //
    // register Foo_Widget widget
    add_action( 'widgets_init', create_function( '', 'register_widget( "wpspo_WidgetJauge" );' ) );


